import * as constants from './constants'
import api from '../../../api/api'
import { message } from 'antd';
import Immutable from 'immutable';
import Utils from '../../../common/utils'
// header模块的store
import { actionCreators } from '../../../components/header/store'

const changePrimary = (result) => ({
	type: constants.CHANGE_PRIMARY,
	value: Immutable.fromJS(result)
})

const changeJunior = (result) => ({
	type: constants.CHANGE_JUNIOR,
	value: Immutable.fromJS(result)
})

const changeHight = (result) => ({
	type: constants.CHANGE_HIGHT,
	value: Immutable.fromJS(result)
})

const changeTeacher = (result) => ({
  type: constants.CHANGE_TEACHER,
  value: Immutable.fromJS(result)
})

const changeSubject = (result) => ({
  type: constants.CHANGE_SUBJECT,
  value: Immutable.fromJS(result)
})

const changeCourseDescript = (result) => ({
  type: constants.CHANGE_COURSEDESCRIPT,
  value: Immutable.fromJS(result)
})

const changeHomeLive = (result) => ({
  type: constants.CHANGE_HOMELIVE,
  value: Immutable.fromJS(result)
})

export const getLesson = (params)=> {
  return (dispatch)=> {
    api.getLesson(params).then(res => {
      if (res.code === 20000) {
        if(params.eduRank === '小学') {
          dispatch(changePrimary(res.data))
        }else if(params.eduRank === '初中') {
          dispatch(changeJunior(res.data))
        }else {
          dispatch(changeHight(res.data))
        }
      } else {
        message.error(res.msg);
      }
    })
  }
}

export const getTeacher = (params) => {
  return (dispatch) => {
    api.getTeacherList(params).then(res => {
      if(res.code === 20000) {
        dispatch(changeTeacher(res.data))
      }
    })
  }
}

export const getAllSubject = () => {
  return (dispatch) => {
    api.getAllSubject().then(res => {
      if(res.code === 20000) {
        Utils.local('subject', res.data)
        dispatch(changeSubject(res.data))
      }
    })
  }
}

export const addCart = (params) => {
  return (dispatch) => {
    api.addToCart(params).then(res => {
      console.log('res', res)
      if(res.code === 20000) {
        message.success('购物车添加成功！')
        // 刷新购物车列表
        dispatch(actionCreators.getCartList())
        dispatch(getLesson({eduRank: '小学', type: 'all'}))
        dispatch(getLesson({eduRank: '初中', type: 'all'}))
        dispatch(getLesson({eduRank: '高中', type: 'all'}))
      }
    })
  }
}

export const loginPhone = (params) => {
  return (dispatch) => {
    // api.login(params).then(res => {
    //   if (res.code === 20000) {
    //     Utils.local('userData', res.data)
    //     dispatch(actionCreators.getCartList())
    //     dispatch(actionCreators.changeLogin(res.data))
    //     window.href="/PHome"
    //   } else {
    //     message.error(res.msg);
    //   }
    // })
    dispatch(actionCreators.Login(params))
    // window.href="/PHome"
  }
}

export const getCourseDescript = (params) => {
  return (dispatch) => {
    api.getCourseDescript(params).then(res => {
      debugger
      if(res.code === 20000) {
        dispatch(changeCourseDescript(res.data))
      }
    })
  }
}


export const getHomeLive= (params) => (dispatch) => {
  api.getHomeLive().then(res => {
    if(res.code === 20000) {
      dispatch(changeHomeLive(res.data))
    }else {
      message.error(res.msg);
    }
  })
}